<template>
  <view class="container">
    <backView />
    <view class="header">
      <text class="title">就诊信息详情</text>
      <view class="actions">
        <nut-button size="small" type="primary" @click="goEdit">编辑</nut-button>
        <nut-button size="small" type="danger" @click="deleteRecord">删除</nut-button>
      </view>
    </view>
    
    <view v-if="loading" class="loading">加载中...</view>
    
    <FormView
      v-else
      :field-config="fieldConfig"
      :form="form"
      :show-buttons="false"
    />
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import backView from '@/components/backView.vue'
import Taro from '@tarojs/taro'
import { request } from '@/service/request'
import FormView from '@/components/FormView.vue'
import { healthSeeDocFields } from './fields'

const fieldConfig = healthSeeDocFields
const loading = ref(false)

const form = reactive({
  name: '',
  idNumber: '',
  recordid: '',
  hospitalName: '',
  department: '',
  doctorName: '',
  visitDate: '',
  diagnosis: '',
  treatment: '',
  medication: '',
  symptoms: '',
  visitType: '',
  visitStatus: '',
  cost: '',
  insuranceType: '',
  followUpDate: '',
  followUpNote: '',
  note: '',
  id: ''
})

onMounted(() => {
  loadRecord()
})

async function loadRecord() {
  const pages = Taro.getCurrentPages()
  const currentPage = pages[pages.length - 1]
  const id = currentPage.options?.id
  
  if (!id) {
    Taro.showToast({ title: '参数错误', icon: 'error' })
    Taro.navigateBack()
    return
  }
  
  loading.value = true
  try {
    const result = await request.get(`/health_records/health_seedoc/healthSeeDocInfo/queryById?id=${id}`, {}, {
      useErrMsg: true
    })
    
    if (result) {
      Object.assign(form, result)
    }
  } catch (error) {
    console.error('加载数据失败:', error)
    Taro.showToast({ title: '加载数据失败', icon: 'error' })
  } finally {
    loading.value = false
  }
}

function goEdit() {
  Taro.navigateTo({
    url: `/pages/historys/edit?id=${form.id}`
  })
}

async function deleteRecord() {
  try {
    const result = await Taro.showModal({
      title: '确认删除',
      content: '确定要删除这条就诊记录吗？',
      confirmText: '删除',
      cancelText: '取消'
    })
    
    if (result.confirm) {
      await request.delete(`/health_records/health_seedoc/healthSeeDocInfo/delete?id=${form.id}`, {}, {
        useErrMsg: true
      })
      
      Taro.showToast({ title: '删除成功', icon: 'success' })
      
      setTimeout(() => {
        Taro.navigateBack()
      }, 1500)
    }
  } catch (error) {
    console.error('删除失败:', error)
    Taro.showToast({ title: '删除失败', icon: 'error' })
  }
}
</script>

<style scoped>
.container {
  padding: 32rpx;
  background: #f6f8fa;
  min-height: 100vh;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40rpx;
}

.title {
  font-size: 48rpx;
  font-weight: bold;
  color: #2563eb;
}

.actions {
  display: flex;
  gap: 16rpx;
}

.loading {
  text-align: center;
  padding: 100rpx 0;
  color: #6b7280;
}
</style>
